package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.entity.AdSensitive;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.DateUtils;
import com.heima.model.admin.dtos.SensitiveDto;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    /**
     * 敏感词列表分页展示
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<AdSensitive> sensitiveList(SensitiveDto dto) {
        Page<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(dto.getSensitives()), AdSensitive::getSensitives, dto.getSensitives());
        Page<AdSensitive> sensitivePage = page(page, queryWrapper);
        PageResult<AdSensitive> sensitiveDtoPageResult = new PageResult<>();
        sensitiveDtoPageResult.setCurrentPage(sensitivePage.getCurrent());
        sensitiveDtoPageResult.setSize(sensitivePage.getSize());
        sensitiveDtoPageResult.setTotal(sensitivePage.getTotal());
        sensitiveDtoPageResult.setData(sensitivePage.getRecords());
        //返回结果
        return sensitiveDtoPageResult;
    }

    /**
     * 新增敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult sensitiveSave(SensitiveDto dto) {
        AdSensitive adSensitive = new AdSensitive();
        //判断数据是否存在
        List<AdSensitive> adSensitiveList = this.list();
        for (AdSensitive ad : adSensitiveList) {
            if (ad.getSensitives().equals(dto.getSensitives())) {
                throw new LeadException(AppHttpCodeEnum.DATA_EXIST);
            }
        }
        //不存在保存
        adSensitive.setSensitives(dto.getSensitives());
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);
        return ResponseResult.ok();
    }

    /**
     * 修改敏感词
     */
    @Override
    public void updateSensitive(SensitiveDto dto) {
        AdSensitive sensitive = new AdSensitive();
        if (dto == null){
            throw new LeadException(AppHttpCodeEnum.UPDATE_ERROR);
        }
        AdSensitive sensitiveDB = this.getById(dto.getId());
        if (sensitiveDB == null){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        sensitive.setId(dto.getId());
        sensitive.setSensitives(dto.getSensitives());
        this.updateById(sensitive);
    }

    @Override
    public void delSensitive(Integer id) {
        if (id == null){
            throw new LeadException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        this.removeById(id);
    }
}
